<?php

namespace app\api\controller\haibao;

use app\admin\model\HaibaoAcc;
use app\admin\model\HaibaoFavor;
use app\admin\model\HaibaoVideo;
use app\admin\model\Xcx;
use app\api\controller\Base;
use Pheanstalk\Pheanstalk;
use think\Request;

/**
 * @author zero
 * @Time 2018/12/26
 *
 * Class User
 * @package app\api\controller\haibao
 *
 * 嗨爆小片用户相关接口
 */
class User extends Base
{
    // 审核控制版本号
    public $control_version = '0.0.0';
    // 当前版本
    public $version;
    // openid
    public $openid;
    // 嗨爆小片用户
    public $user = null;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->openid = $request->param('openid');
        if ($this->openid) {
            $this->user = HaibaoAcc::get(['openid' => $this->openid]);
            if (!$this->user) {
                $this->error('no user');
            }
        }
        $this->version = $this->request->param('version') ?: '1.0.0';
    }

    // 登录接口
    public function login()
    {
        $gh_id = $this->request->post('gh_id');
        $code = $this->request->post('code');
        // 获取openid (微信服务器交互)
        $openid = $this->get_openid($gh_id, $code);
        if ($openid) {
            $user = HaibaoAcc::get(['openid' => $openid]);
            if (!$user) {
                $user = new HaibaoAcc();
                $user->openid = $openid;
                $user->lock = 0;
                $user->is_formid = 1;
            } else {
                $begin_today = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
                if ($user->prev_formid < $begin_today) {
                    $user->is_formid = 1;
                }
            }
            $user->save();
            $data['openid'] = $openid;
            $data['userid'] = $user->id;
            $data['lock'] = $user->lock;
            $data['is_formid'] = $user->is_formid ? true : false;
            $this->result('login success', $data);
        } else {
            $this->error('login fail');
        }
    }

    // 投诉接口
    public function complaint()
    {
        $this->user->lock = 1;
        $this->user->save();
        return 'true';
    }

    // 配置
    public function config()
    {
        $flags['videoflag'] = true; // 审核控制
        $flags['adflag'] = true; // 广告控制
        $flags['txflag'] = true; // 腾讯视频插件控制
        if ($this->version == $this->control_version) {
            $flags['videoflag'] = false;
            $flags['adflag'] = false;
            $flags['txflag'] = false;
        }
        $this->result('flag', $flags);
    }

    // 记录用户分享视频
    public function handle_share()
    {
        die();
    }

    //  查询是否收藏
    public function fetch_favor()
    {
        $favor_id = $this->request->post('collect_id');
        $account_id = $this->request->post('user_id');
        if ($f = HaibaoFavor::get(['account_id' => $account_id, 'favor_id' => $favor_id])) {
            $msg = 'ok';
        } else {
            $msg = 'false';
        }
        $this->result($msg);
    }

    // 添加收藏
    public function add_favor()
    {
        $favor_id = $this->request->post('collect_id');
        $account_id = $this->request->post('user_id');
        $f = new HaibaoFavor();
        $f->account_id = $account_id;
        $f->favor_id = $favor_id;
        if ($f->save() !== false) {
            $video = HaibaoVideo::get($favor_id);
            $video->collect_num += 1;
            $video->save();
            $this->success('ok');
        }
        $this->error('fail');
    }

    // 删除收藏
    public function del_favor()
    {
        $favor_id = $this->request->post('collect_id');
        $account_id = $this->request->post('user_id');
        $f = HaibaoFavor::get(['account_id' => $account_id, 'favor_id' => $favor_id]);
        if ($f && $f->delete() !== false) {
            $video = HaibaoVideo::get($favor_id);
            $video->collect_num -= 1;
            $video->save();
            $this->success('ok');
        }
        $this->error('fail');
    }

    // 获取收藏列表
    public function get_favor_list()
    {
        if ($this->version == $this->control_version) {
            $this->result('list', array());
        }

        $account_id = $this->request->post('user_id');
        $favor_list = HaibaoFavor::where('account_id', $account_id)->column('favor_id, createtime');
        $list = array();

        foreach ($favor_list as $id => $time) {
            $item = HaibaoVideo::get($id);
            if ($item) {
                $item->createtime = date('Y年m月d日', $time);
                $list[$item->createtime][] = $item;
            }
        }
        $this->result('list', array_reverse($list));
    }

    // 处理收到formid,并创建发送任务
    public function handler_formid()
    {
        // 模板消息数据
        $tmp = array(
            'template_id' => '0PCXpAXpejvUFRSvQ7i6xjoBbWv0xpAQfeDnpZFNbjo',
            'page' => 'pages/index/index',
            'data' => array(
                'keyword1' => '最新精彩视频',
                'keyword2' => '点击进入小程序,查看详细信息',
            )
        );
        $ghid = $this->request->post('ghid');
        $openid = $this->request->post('openid');
        $formid = $this->request->post('formid');

        if ($formid) {
            $acc = HaibaoAcc::get(['openid' => $openid]);
            $acc->is_formid = 0;
            $acc->prev_formid = time();
            $acc->save();

            $xcx = Xcx::get(['ghid' => $ghid]);

            $data['appid'] = $xcx->appid;
            $data['secret'] = $xcx->secret;
            $data['openid'] = $openid;
            $data['formid'] = $formid;
            $data['tmp'] = $tmp;

            $ser = new Pheanstalk('127.0.0.1');
            if ($ser->useTube('xcx_tmp')->put(json_encode($data), 2, 86000, 60)) {
                $this->success('ok');
            } else {
                $this->error('error');
            }
        }
    }
}
